package de.hpi.generationmanager;

import de.hpi.generationmanager.config.SettingsRoot;
import de.hpi.generationmanager.tesma.TesmaGenerator;
import de.hpi.generator.generator.StartArgs;
import de.hpi.generationmanager.tesma.JSONToTesmaConverter;
import de.hpi.generationmanager.tesma.data.TesmaDataTable;
import de.hpi.generationmanager.tools.JSONConfigurationParser;
import de.hpi.generator.config.ConfigParser;
import de.hpi.generator.model.Database;

public class GenerationManager {

    /**
     * @param args
     */
    public static Database preProcessIntergratedConfig(String[] args) throws Exception {
        StartArgs aStartupArguments = new StartArgs(args);
        SettingsRoot aGeneralConfig = JSONConfigurationParser.parseConfig(aStartupArguments.getConfigPath());

        generateTesmaData(aStartupArguments, aGeneralConfig);
        importTesmaDataIntoDatabase(aStartupArguments, aGeneralConfig);
        
        return ConfigParser.parseDatabaseModel(aStartupArguments, aGeneralConfig.CSGenerationMode);
    }

    protected static void generateTesmaData(StartArgs aStartupArguments, SettingsRoot aGeneralConfig) throws Exception {
        StartArgs aTesmaStartupArguments = createTesmaStartupArguments(aStartupArguments, aGeneralConfig);
        generateTesmaDataFromConfig(createTesmaConfigFile(aTesmaStartupArguments, aGeneralConfig), aTesmaStartupArguments);
    }

    protected static void importTesmaDataIntoDatabase(StartArgs aStartupArguments, SettingsRoot aGeneralConfig) throws Exception {
        String aTesmaDataPath = getTesmaOutputPath(aStartupArguments, aGeneralConfig);
        Database aTesmaDataBase = new Database(aGeneralConfig.CSGenerationMode, aStartupArguments.getOutputPath());

        for (TesmaDataTable aTesmaTableFile : TesmaDataTable.findTesmaDataTables(aTesmaDataPath, aTesmaDataBase)) {
            aTesmaTableFile.loadDataInToColumns();
        }
    }
    
    private static StartArgs createTesmaStartupArguments(StartArgs aStartupArguments, SettingsRoot aGeneralConfig) throws Exception {
        StartArgs aTesmaStartupArguments = new StartArgs(
                new String[]{
                    aStartupArguments.getGenerationMode(),
                    aStartupArguments.getConfigPath(),
                    getTesmaOutputPath(aStartupArguments, aGeneralConfig)
                });
        return aTesmaStartupArguments;
    }

    protected static void generateTesmaDataFromConfig(String aTesmaConfigFileName, StartArgs aStartupArguments) throws Exception {
        StartArgs aGenerationParameters = new StartArgs(new String[]{"integrated_config", aTesmaConfigFileName, aStartupArguments.getOutputPath()});
        TesmaGenerator.generateData(aGenerationParameters);
    }

    protected static String createTesmaConfigFile(StartArgs aStartupArguments, SettingsRoot aGeneralConfig) throws Exception {
        String aTesmaConfigFileName = getTesmaConfigFileName(aStartupArguments, aGeneralConfig);
        JSONToTesmaConverter.saveModelAsTesmaConfig(aGeneralConfig.TesmaGenerationMode, aTesmaConfigFileName);
        return aTesmaConfigFileName;
    }

    protected static String getTesmaConfigFileName(StartArgs aStartArgs, SettingsRoot aRoot) {
        String aTesmaConfigFileName = String.format(
                "%s\\%s",
                aStartArgs.getOutputPath(),
                aRoot.TesmaGenerationMode.name);
        return aTesmaConfigFileName;
    }

    protected static String getTesmaOutputPath(StartArgs aStartArgs, SettingsRoot aRoot) {
        String aTesmaConfigFileName = String.format(
                "%s\\tesmaData",
                getDatabaseOutputPath(aStartArgs, aRoot));
        return aTesmaConfigFileName;
    }

    protected static String getDatabaseOutputPath(StartArgs aStartArgs, SettingsRoot aRoot) {
        String aTesmaConfigFileName = String.format(
                "%s\\%s",
                aStartArgs.getOutputPath(),
                aRoot.CSGenerationMode.name);
        return aTesmaConfigFileName;
    }
}
